package com.jin290.mqlibrary;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class SeleCoursectPicPopupWindow extends PopupWindow {


    private View mMenuView;
    private TextView tv_alert_cancel;
    private TextView tv_alert_ok;
    private GridView grd_course_dialog;
    private TextView tv_alert_dele;
    private CourseDialogAdapter courseDialogAdapter;
    private RelativeLayout rel_dialog;

    private List<String> mlist;
    private String s = "语文,外语,数学,体育,音乐,计算机,自习,美术,自定义";


    /**
     * @param context
     * @param itemsOnClick
     */
    public SeleCoursectPicPopupWindow(final Activity context, OnClickListener itemsOnClick) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context
                .LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.course_dialog, null);
        tv_alert_cancel = (TextView) mMenuView.findViewById(R.id.tv_alert_cancel);
        tv_alert_ok = (TextView) mMenuView.findViewById(R.id.tv_alert_ok);
        grd_course_dialog = (GridView) mMenuView.findViewById(R.id.grd_course_dialog);
        tv_alert_dele = (TextView) mMenuView.findViewById(R.id.tv_alert_dele);
        rel_dialog = (RelativeLayout) mMenuView.findViewById(R.id.rel_dialog);


        courseDialogAdapter = new CourseDialogAdapter(context);
        mlist = new ArrayList<>();

        if (SPUtils.contains(context, "class")) {
            s = SPUtils.getAll(context).get("class").toString();
        } else {
            SPUtils.put(context, "class", s);
        }
        for (String ss : s.split(",")) {
            mlist.add(ss);
        }

        courseDialogAdapter.getdata(mlist);
        grd_course_dialog.setAdapter(courseDialogAdapter);

//        //设置按钮监听
        tv_alert_dele.setOnClickListener(itemsOnClick);
        tv_alert_ok.setOnClickListener(itemsOnClick);
        tv_alert_cancel.setOnClickListener(itemsOnClick);
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.popup_bottom_anim);
        //实例化一个ColorDrawapopup_bottom_ani
        ColorDrawable dw = new ColorDrawable(0x000000ff);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();

                    } else {

                    }
                }
                return true;
            }
        });

    }

    public CourseDialogAdapter getAdapter() {
        return courseDialogAdapter;
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        if (onItemClickListener == null) return;
        grd_course_dialog.setOnItemClickListener(onItemClickListener);
    }

    public void setMposition(int mposition) {
        courseDialogAdapter.setMposition(mposition);
        courseDialogAdapter.notifyDataSetChanged();
    }

    public List<String> getdata() {
        return mlist;
    }

    public void setdata(List<String> L) {

        courseDialogAdapter.getdata(L);
        courseDialogAdapter.notifyDataSetChanged();
    }

    public String getv() {
        int position = courseDialogAdapter.getMposition();

        if (position == 10086) {
            return "";
        }
        return mlist.get(position);
    }


}
